package com.shark.security.jwt.persistent.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.shark.security.jwt.persistent.entity.UserEntity;

@Repository
public class UserRepository {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;

	private static final String SQL_GET_BY_LOGIN_NAME = "select id, login_name, user_name, password, is_freeze, user_type from t_sys_user where login_name = :loginName and flag = 0";

	/**
	 * 根据用户登录名的用户数据
	 * @param LoginName
	 * @return
	 */
	public UserEntity getUserByLoginName(String LoginName) {
		Map<String, Object> mapParam = new HashMap<>();
		mapParam.put("loginName", LoginName);
		try {
			return jdbcTemplate.queryForObject(SQL_GET_BY_LOGIN_NAME, mapParam, new RowMapper<UserEntity>() {
	
				@Override
				public UserEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
					UserEntity u = new UserEntity();
					u.setId(rs.getInt("id"));
					u.setLoginName(rs.getString("login_name"));
					u.setUserName(rs.getString("user_name"));
					u.setPassword(rs.getString("password"));
					u.setIsFreeze(rs.getByte("is_freeze"));
					u.setUserType(rs.getInt("user_type"));
					return u;
				}
			});
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

}
